package flc.ast.adapter;

import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.stark.calculator.databinding.ItemTaxPickItemBinding;
import com.stark.calculator.tax.model.PickItem;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import flc.ast.R;
import flc.ast.databinding.ItemSelCityBinding;
import stark.common.basic.adapter.BaseDBRVAdapter;
import stark.common.basic.utils.ViewUtil;

public class MyPickItemAdapter extends BaseDBRVAdapter<PickItem, ItemSelCityBinding> {

    public MyPickItemAdapter() {
        super(R.layout.item_sel_city, 0);
    }

    @Override
    public void onBindViewHolder(@NotNull BaseDataBindingHolder<ItemSelCityBinding> holder, int position) {
        super.onBindViewHolder(holder, position);
        ItemSelCityBinding binding = holder.getDataBinding();
        int visibility = View.GONE;
        if (position == 0) {
            visibility = View.VISIBLE;
        } else {
            PickItem curItem = getData().get(position);
            PickItem preItem = getData().get(position - 1);
            if (!curItem.getLetter().equals(preItem.getLetter())) {
                visibility = View.VISIBLE;
            }
        }
        binding.tvLetter.setVisibility(visibility);
    }

    @Override
    protected void convert(@NotNull BaseDataBindingHolder<ItemSelCityBinding> holder, PickItem item) {
        ItemSelCityBinding binding = holder.getDataBinding();
        ViewUtil.setViewText(binding.tvLetter, item.getLetter());
        ViewUtil.setViewText(binding.tvName, item.getName());
    }

    /**
     * 获取letter在字母列表中的第一个出现的位置
     *
     * @param letter
     * @return
     */
    public int getFirstPosBy(String letter) {
        if (TextUtils.isEmpty(letter)) {
            return -1;
        }
        List<PickItem> itemList = getData();
        if (itemList == null || itemList.size() == 0) {
            return -1;
        }
        String indexLetter = null;
        for (int i = 0; i < itemList.size(); i++) {
            indexLetter = itemList.get(i).getLetter();
            if (indexLetter != null && indexLetter.contains(letter)) {
                return i;
            }
        }
        return -1;
    }

}

